package com.burt.springcloudeurekaconsumerdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * DcController
 * 
 * @author burt
 * @date 2018/7/16 19:20
 */
@RestController
public class DcController {

    private final LoadBalancerClient loadBalancerClient;

    private final RestTemplate       restTemplate;

    @Autowired
    public DcController(LoadBalancerClient loadBalancerClient, RestTemplate restTemplate) {
        this.loadBalancerClient = loadBalancerClient;
        this.restTemplate = restTemplate;
    }

    @GetMapping("/consumer")
    public String dc() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
        String url = serviceInstance.getUri() + "/dc";
        System.out.println(url);
        return restTemplate.getForObject(url, String.class);
    }

}
